using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;

namespace WPChanger
{
	/// <summary>
	/// Summary description for Options.
	/// </summary>
	public class frmOptions : System.Windows.Forms.Form
	{
		private System.Windows.Forms.GroupBox grpDir;
		private System.Windows.Forms.Button btnPickWPDir;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.GroupBox grpTimer;
		private System.Windows.Forms.TextBox txtTimer;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckBox chkTimer;
		private System.Windows.Forms.CheckBox chkAuto;
		private System.Windows.Forms.TextBox txtDir;
		private System.Windows.Forms.Button btnApply;
		private System.Windows.Forms.Button btnCancel;

		private const string REG_RUN = @"Software\Microsoft\Windows\CurrentVersion\Run";
		private const string CHANGEWP = " -change";
		private const int HKLM = 1; //section
		private const int HKCU = 2; //section
		private static RegistryKey key;
		private string _FileName = "";
		private string _ExePath = "";
		private Settings _Settings = null;
		private System.Windows.Forms.GroupBox grpPattern;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtPattern;		

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmOptions(string fileName, string exePath)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			_ExePath = exePath;
			_FileName = fileName;
			if (File.Exists(_FileName))
			{
				_Settings = SettingsManager.loadSettings(_FileName);
			}
			else
			{
				_Settings = new Settings();
			}
			initialize();
		}

		public frmOptions(Settings aSettings, string fileName, string exePath)
		{
			InitializeComponent();
			_ExePath = exePath;
			_FileName = fileName;
			_Settings = aSettings;
			initialize();
		}

		private void initialize()
		{
			txtDir.Text = _Settings.WPDirectory;
			chkAuto.Checked = _Settings.AutoRun;
			chkTimer.Checked = _Settings.EnableTimer;
			txtPattern.Text = _Settings.PatternMatch;
			txtTimer.Text = Convert.ToString(_Settings.TimerInterval);
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmOptions));
			this.grpDir = new System.Windows.Forms.GroupBox();
			this.btnPickWPDir = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.txtDir = new System.Windows.Forms.TextBox();
			this.grpTimer = new System.Windows.Forms.GroupBox();
			this.txtTimer = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.chkTimer = new System.Windows.Forms.CheckBox();
			this.chkAuto = new System.Windows.Forms.CheckBox();
			this.btnApply = new System.Windows.Forms.Button();
			this.btnCancel = new System.Windows.Forms.Button();
			this.grpPattern = new System.Windows.Forms.GroupBox();
			this.txtPattern = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.grpDir.SuspendLayout();
			this.grpTimer.SuspendLayout();
			this.grpPattern.SuspendLayout();
			this.SuspendLayout();
			// 
			// grpDir
			// 
			this.grpDir.Controls.Add(this.btnPickWPDir);
			this.grpDir.Controls.Add(this.label1);
			this.grpDir.Controls.Add(this.txtDir);
			this.grpDir.Location = new System.Drawing.Point(5, 0);
			this.grpDir.Name = "grpDir";
			this.grpDir.Size = new System.Drawing.Size(488, 64);
			this.grpDir.TabIndex = 7;
			this.grpDir.TabStop = false;
			// 
			// btnPickWPDir
			// 
			this.btnPickWPDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnPickWPDir.Location = new System.Drawing.Point(456, 33);
			this.btnPickWPDir.Name = "btnPickWPDir";
			this.btnPickWPDir.Size = new System.Drawing.Size(24, 20);
			this.btnPickWPDir.TabIndex = 5;
			this.btnPickWPDir.Text = "...";
			this.btnPickWPDir.Click += new System.EventHandler(this.btnPickWPDir_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(10, 15);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(112, 17);
			this.label1.TabIndex = 4;
			this.label1.Text = "Wallpaper directory";
			// 
			// txtDir
			// 
			this.txtDir.Location = new System.Drawing.Point(5, 33);
			this.txtDir.Name = "txtDir";
			this.txtDir.Size = new System.Drawing.Size(443, 20);
			this.txtDir.TabIndex = 3;
			this.txtDir.Text = "";
			// 
			// grpTimer
			// 
			this.grpTimer.Controls.Add(this.txtTimer);
			this.grpTimer.Controls.Add(this.label2);
			this.grpTimer.Controls.Add(this.chkTimer);
			this.grpTimer.Location = new System.Drawing.Point(5, 64);
			this.grpTimer.Name = "grpTimer";
			this.grpTimer.Size = new System.Drawing.Size(355, 40);
			this.grpTimer.TabIndex = 6;
			this.grpTimer.TabStop = false;
			// 
			// txtTimer
			// 
			this.txtTimer.Location = new System.Drawing.Point(248, 12);
			this.txtTimer.Name = "txtTimer";
			this.txtTimer.TabIndex = 6;
			this.txtTimer.Text = "";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(104, 14);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(136, 13);
			this.label2.TabIndex = 0;
			this.label2.Text = "Timer interval (seconds):";
			// 
			// chkTimer
			// 
			this.chkTimer.Location = new System.Drawing.Point(11, 9);
			this.chkTimer.Name = "chkTimer";
			this.chkTimer.Size = new System.Drawing.Size(93, 24);
			this.chkTimer.TabIndex = 5;
			this.chkTimer.Text = "Enable Timer";
			this.chkTimer.CheckedChanged += new System.EventHandler(this.chkTimer_CheckedChanged);
			// 
			// chkAuto
			// 
			this.chkAuto.Location = new System.Drawing.Point(376, 75);
			this.chkAuto.Name = "chkAuto";
			this.chkAuto.Size = new System.Drawing.Size(96, 16);
			this.chkAuto.TabIndex = 8;
			this.chkAuto.Text = "Run at startup";
			// 
			// btnApply
			// 
			this.btnApply.Location = new System.Drawing.Point(336, 160);
			this.btnApply.Name = "btnApply";
			this.btnApply.Size = new System.Drawing.Size(75, 24);
			this.btnApply.TabIndex = 9;
			this.btnApply.Text = "Apply";
			this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(417, 160);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(75, 24);
			this.btnCancel.TabIndex = 10;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// grpPattern
			// 
			this.grpPattern.Controls.Add(this.txtPattern);
			this.grpPattern.Controls.Add(this.label3);
			this.grpPattern.Location = new System.Drawing.Point(5, 104);
			this.grpPattern.Name = "grpPattern";
			this.grpPattern.Size = new System.Drawing.Size(488, 48);
			this.grpPattern.TabIndex = 11;
			this.grpPattern.TabStop = false;
			// 
			// txtPattern
			// 
			this.txtPattern.Location = new System.Drawing.Point(248, 16);
			this.txtPattern.Name = "txtPattern";
			this.txtPattern.Size = new System.Drawing.Size(232, 20);
			this.txtPattern.TabIndex = 1;
			this.txtPattern.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 16);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(232, 23);
			this.label3.TabIndex = 0;
			this.label3.Text = "Insert Pattern Matching for Wallpaper search:";
			// 
			// frmOptions
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(496, 186);
			this.Controls.Add(this.grpPattern);
			this.Controls.Add(this.btnCancel);
			this.Controls.Add(this.btnApply);
			this.Controls.Add(this.chkAuto);
			this.Controls.Add(this.grpDir);
			this.Controls.Add(this.grpTimer);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmOptions";
			this.Text = "Options";
			this.grpDir.ResumeLayout(false);
			this.grpTimer.ResumeLayout(false);
			this.grpPattern.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void chkTimer_CheckedChanged(object sender, System.EventArgs e)
		{
			txtTimer.Enabled = chkTimer.Checked;
		}

		private void btnApply_Click(object sender, System.EventArgs e)
		{
			_Settings.WPDirectory = txtDir.Text.Trim();
			_Settings.AutoRun = chkAuto.Checked;
			this.autorun(_ExePath + CHANGEWP,_Settings.AutoRun);
			_Settings.EnableTimer = chkTimer.Checked;
			_Settings.TimerInterval = Convert.ToInt32(txtTimer.Text.Trim());
			_Settings.PatternMatch = txtPattern.Text.Trim();
			SettingsManager.saveSettings(_FileName,_Settings);
			this.Close();
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void btnPickWPDir_Click(object sender, System.EventArgs e)
		{
			Shell32.Shell picker = new Shell32.ShellClass();
			Shell32.Folder folder = picker.BrowseForFolder((int)this.Handle, "Resource Updater", 0, 0);

			if (folder != null)
			{
				Shell32.FolderItem fi = (folder as Shell32.Folder3).Self;
				txtDir.Text = fi.Path;
			}
		}

		
	
		/*****************************
		 * AUTORUN CODE
		 * ***************************/
		private void autorun(string exePath, bool val)
		{
			if (val == true)
			{
				setValue(HKCU, REG_RUN, exePath);
			}
			else
			{		  
				deleteValue(HKCU, REG_RUN, exePath);
			}
		}
		public static RegistryKey getKey(int Section)
		{
			switch (Section)
			{
				case 1:
					// HKEY_LOCALMACHINE
					key = Registry.LocalMachine;
					break;
				case 2:
					// current user
					key = Registry.CurrentUser;
					break;
				case 3:
					// classes root
					key = Registry.ClassesRoot;
					break;
				case 4:
					// users
					key = Registry.Users;
					break;
				default:
					key = Registry.LocalMachine;
					break;
			}
			return key;
		}
		
		private static bool setValue(int Section, string Location, string Value)
		{
			key = getKey(Section);
			try
			{
				key.OpenSubKey(Location);
				key.SetValue(Value, Value);
			}
			catch
			{
				return false;
			}
			finally
			{
				key.Close();
			}
			return true;
		}
		
		private static bool deleteValue(int Section, string Location, string Value)
		{
			key = getKey(Section);
			try
			{
				key.OpenSubKey(Location);
				key.DeleteValue(Value, false);
			}
			catch
			{
				return false;
			}
			finally
			{
				key.Close();
			}
			return true;
		}
	}
}
